<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

use think\Db;

if (!function_exists('checkIp4')) {
    //判定是否为合法ip4地址
    function checkIp4($ip)
    {
        if (preg_match('/^((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1 -9]?\d))))$/', $ip)) {
            return true;
        } else {
            return false;
        }
    }
}

if (!function_exists('getLinkMacroParameters')) {
    function getLinkMacroParameters($type, $cid, $linkId = 0)
    {
        $arr[1] = config('monitor_domain') . "/monitoring_receive?adtype=ks&channel_id={$cid}&ip=__IP__&ua=__UA__&callback=__CALLBACK__&oaidmd5=__OAID2__&adid=__ACCOUNTID__&kaid=__AID__&kdid=__DID__&kcid=__CID__";
        $arr[2] = "";
        $arr[3] = config('monitor_domain') . "/monitoring_receive?adtype=jl&channel_id={$cid}&callback=__CALLBACK_PARAM__&request_id=__REQUEST_ID__&campaign_id=__PROJECT_ID__&aid=__PROMOTION_ID__&adid=__ADVERTISER_ID__&ip=__IP__&ua=__UA__&mid1=__MID1__&mid2=__MID2__&mid3=__MID3__&mid4=__MID4__&mid5=__MID5__&mid6=__MID6__";
        $arr[4] = config('monitor_domain') . "/monitoring_receive?adtype=baidu&channel_id={$cid}&ip=__IP__&ua=__UA__&ts=__TS__&userid=__USER_ID__&pid=__PLAN_ID__&aid=__IDEA_ID__&click_id=__CLICK_ID__&callback_url=__CALLBACK_URL__&bd_vid=__BD_VID__&oaidmd5=__OAID_MD5__";
        $arr[5] = config('monitor_domain') . "/monitoring_receive?adtype=uc&channel_id={$cid}&imei_sum={IMEI_SUM}&imei_sum1={IMEI_SUM1}&oaid={OAID}&oaidmd5={OAID_SUM}&ts={TS}&callback={CALLBACK}&ip={IP}&aid={AID}&cid={CID}&ua={UA}&advertiser_id={ACID}&campaign_id={GID}";
        $arr[6] = config('monitor_domain') . "/monitoring_receive?adtype=vivo&channel_id={$cid}&creativeId=__CREATIVEID__&reqId=__REQUESTID__&oaid=__OAID__&oaidmd5=__OAIDMD5__";
        $arr[7] = config('monitor_domain') . "/monitoring_receive?adtype=oppo&channel_id={$cid}&ip=__IP__&ua=__UA__" . '&imei=$im$&oaid=__OAID__&req_id=$req$&advertiser_id=$ownerid$&campaign_id=$groupid$&aid=$planid$&cid=$ad$';
        $arr[8] = config('monitor_domain') . "/monitoring_receive?adtype=bilibili&channel_id={$cid}&ip=__IP__&ua=__UA__&advertiser_id=__ACCOUNTID__&campaign_id=__CAMPAIGNID__&aid=__UNITID__&cid=__CREATIVEID__&imei=__IMEI__&oaidmd5=__OAIDMD5__&track_id=__TRACKID__";
       /* if (in_array($type, [5, 6, 8]) && $linkId) {
            //vivo和uc用ip匹配，带个链接Id（匹配用户更精准一点）
            $arr[$type] = $arr[$type] . "&linkId={$linkId}";
        }*/
        return $arr[$type];
    }
}

if (!function_exists('getLinkHapMacroParameters')) {
    function getLinkHapMacroParameters($type = 1)
    {
        $arr[1][0]     = '&adtype=ks&ip=__IP__&callback=__CALLBACK__&oaidmd5=__OAID2__&adid=__ACCOUNTID__&kaid=__AID__&kdid=__DID__&kcid=__CID__';
        $arr[2][0]     = '&adtype=jlq&campaign_id=__CAMPAIGN_ID__&aid=__AID__&adid=__ADVERTISER_ID__&imei=__IMEI__&androidid=__ANDROIDID__&oaid=__OAID__&oaidmd5=__OAID_MD5__&mid1=__MID1__&mid2=__MID2__&mid3=__MID3__&mid4=__MID4__&mid5=__MID5__&mid6=__MID6__&clickid=__CLICKID__';
        $arr[2][1]     = '&adtype=jlq&campaign_id=__PROJECT_ID__&aid=__PROMOTION_ID__&adid=__ADVERTISER_ID__&imei=__IMEI__&androidid=__ANDROIDID__&oaid=__OAID__&oaidmd5=__OAID_MD5__&mid1=__MID1__&mid2=__MID2__&mid3=__MID3__&mid4=__MID4__&mid5=__MID5__&mid6=__MID6__&clickid=__CLICKID__';
        $arr[3][0]     = '&adtype=jl&request_id=__REQUEST_ID__';
        $arr[4][0]     = '&adtype=baidu&bd_vid=__BD_VID__&oaid=__OAID__&dplink={{DEEPLINK_BACKURL}}';
        $arr[5][0]     = '&adtype=uc';
        $arr[6][0]     = '&adtype=vivo&creativeId=__CREATIVEID__&reqId=__REQUESTID__&oaid=__OAID__&oaidmd5=__OAIDMD5__';
        $arr[7][0]     = '&adtype=oppo&req_id=$req$';
        $arr[8][0]     = '&adtype=bilibili&ip=__IP__&advertiser_id=__ACCOUNTID__&campaign_id=__CAMPAIGNID__&aid=__UNITID__&cid=__CREATIVEID__&imei=__IMEI__&oaidmd5=__OAIDMD5__&track_id=__TRACKID__';
        return $arr[$type];
    }
}

if (!function_exists('getH5JumpLink')) {
    function getH5JumpLink($type = 1)
    {
        $arr[1][0]     = "";
        $arr[2][0]     = "";
        $arr[3][0]     = "&adtype=jl&request_id=__REQUEST_ID__";
        $arr[4][0]     = "";
        $arr[5][0]     = "";
        $arr[6][0]     = "";
        $arr[7][0]     = "";
        $arr[8][0]     = "";
        return $arr[$type];
    }
}

if (!function_exists('getH5Jump2Link')) {
    function getH5Jump2Link($type = 1)
    {
        $arr[1][0]     = "";
        $arr[2][0]     = "";
        $arr[3][0]     = "&adtype=jl&request_id=__REQUEST_ID__";
        $arr[4][0]     = "";
        $arr[5][0]     = "";
        $arr[6][0]     = "";
        $arr[7][0]     = "";
        $arr[8][0]     = "";
        return $arr[$type];
    }
}


if (!function_exists('getBackhaulLoopRatio')) {
    function getBackhaulLoopRatio()
    {
        $arr = ['1:1','1:2','1:3','1:4','1:5','1:6','1:7','1:8','1:9','1:10','1:11','1:12','1:13','1:14','1:15','2:1','3:1','4:1','5:1'];
        return $arr;
    }
}

if (!function_exists('getByWithdrawalAmount')) {
    //平台累计提现总额
    function getByWithdrawalAmount()
    {
        $commission_amount = Db::table('cms_withdrawal_record')->sum('money');
        return $commission_amount;
    }
}

if (!function_exists('getByCommissionAmount')) {
    //平台累计分成总额
    function getByCommissionAmount()
    {
        $commission_amount = Db::table('rp_income_summary_daily_agent')->sum('commission_amount');
        return $commission_amount;
    }
}

if (!function_exists('getByWithdrawalAmountAgent')) {
    //代理累计提现总额
    function getByWithdrawalAmountAgent($agent_id)
    {
        $commission_amount = Db::table('cms_withdrawal_record')->where('agent_id',$agent_id)->sum('money');
        return $commission_amount;
    }
}
if (!function_exists('getByCommissionAmountAgent')) {
    //代理累计分成总额
    function getByCommissionAmountAgent($agent_id)
    {
        $commission_amount = Db::table('rp_income_summary_daily_agent')->where('agent_id',$agent_id)->sum('commission_amount');
        return $commission_amount;
    }
}

if (!function_exists('getByBankStatusName')) {
    //获取银行卡记录状态名称
    function getByBankStatusName($status)
    {
        $update_status = ['1'=>'<span style="color: red">待审核</span>','2'=>'<span style="color: green">已审核</span>'];
        return $update_status[$status];
    }
}

if (!function_exists('getChannelId')) {
    /*
     * 获取投放号ID
     * */
    function getChannelId($params,$roleName,$muid)
    {

        if(isset($params['channel_id']) && strlen($params['channel_id']) >= 1)
        {
            return [$params['channel_id']];
        }

        if ($roleName == 'agent') {
            $channel_ids = Db::table('cms_manager_user')->where('parent_id','=',$muid)
                ->column('id');
            return $channel_ids ? $channel_ids : [0];
        }

        if ($roleName == 'channel') {
            return [$muid];
        }
    }
}

if (!function_exists('getPayType')) {
    /**
     * 获取用户信息表表名
     * param int $type  类型名称
     */
    function getPayType($type)
    {
        $str = '';
        $title = [1 => '微信', 2 => '支付宝', 3 => 'BOOST', 4 => 'ShopeePay'];
        if($type >= 1)
        {
            $str = $title[$type];
        }
        return $str;
    }
}

/**
 * 金钱的格式化，保留2位小数显示（单位是分的）
 * @param int $money
 * @return float
 * @Author: whh
 */
function moneyFormat($money = 0)
{
    return floatval(number_format($money / 100, 2, '.', ''));
}

/**
 * 获取下属用户
 * @param $alias
 * @return \app\backstage\model\ManagerUser[]|\think\Collection
 * @Author: whh
 */
function sonUsers($alias)
{
    $where = [];
    if ($alias != '') {
        $findRole = \app\backstage\model\ManagerRole::getRoleByAlias($alias);
        if ($findRole) {
            $where[] = ['role_id', '=', $findRole['id']];
        }
    }
    $role = request()->_role;
    $user = request()->_user;
    if ($role->alias == 'agent' || $role->alias == 'channel' || $role->alias == 'eb') {
        if ($role->alias == 'eb' && $alias == 'channel') {
            $uModel = new \app\backstage\model\ManagerUser();
            $agentIds = $uModel->sonUsersIds($user->id);
            $where[] = ['parent_id', 'in', $agentIds];
        } else {
            $where[] = ['parent_id', '=', $user->id];
        }
    }
    $query = new \app\backstage\model\ManagerUser();
    $query = $query->field("id,account name")->where($where);
    $data = $query->order("id desc")->select()->toArray();
    return $data;
}

if (!function_exists('getAgents')) {
    function getAgents()
    {
        $role_id    = Db::table('cms_manager_role')->where('alias','agent')->value('id');
        $agents     = Db::table('cms_manager_user')->where('role_id',$role_id)
                        ->field('id,account,remarks')->select();

        return $agents;
    }
}
